package pe.edu.upc.dew.tddjdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

import org.apache.commons.dbutils.DbUtils;

public class JdbcUtils {

	public static Connection getConnection() {
		Connection connection = null;
		try {
			Class.forName("org.hsqldb.jdbc.JDBCDriver");
			connection = DriverManager.getConnection("jdbc:hsqldb:mem:test",
					"sa", "");
		} catch (Exception e) {
			throw new IllegalArgumentException("Error al obtener la conexion",
					e);
		}
		return connection;
	}

	public static void create() {
		Connection connection = getConnection();
		try {
			Statement st = connection.createStatement();
			st.execute("create table usuario(username varchar(50) primary key, password varchar(50), nombre varchar(50))");
			st.execute("insert into usuario(username, password, nombre) values('admin', '1234', 'Administrador')");
		} catch (SQLException e) {
			throw new IllegalArgumentException(
					"Error al crear la base de datos", e);
		} finally {
			DbUtils.closeQuietly(connection);
		}
	}
}